// deploy-upgradeable.js
// 部署可升级合约的脚本，包括代理合约和初始实现合约

const hre = require("hardhat");
const { ethers, upgrades } = require("hardhat");

async function main() {
  console.log("开始部署可升级合约...");

  // 获取部署账户
  const [deployer] = await ethers.getSigners();
  console.log(`使用部署账户: ${deployer.address}`);

  // 部署AuctionNFT合约
  console.log("\n部署AuctionNFT合约...");
  const AuctionNFT = await ethers.getContractFactory("AuctionNFT");
  const auctionNFT = await AuctionNFT.deploy(deployer.address);
  await auctionNFT.waitForDeployment();
  console.log(`AuctionNFT部署完成，地址: ${await auctionNFT.getAddress()}`);

  // 部署AuctionFactory可升级合约
  console.log("\n部署AuctionFactory可升级合约...");
  const AuctionFactory = await ethers.getContractFactory("AuctionFactory");
  
  // 使用OpenZeppelin upgrades插件部署可升级合约
  // 这会自动部署代理、实现合约和代理管理员合约
  const auctionFactory = await upgrades.deployProxy(
    AuctionFactory, 
    [
      deployer.address, // 初始拥有者
      deployer.address, // 费用接收地址
      ethers.ZeroAddress // 先使用零地址作为价格预言机，稍后可以更新
    ],
    { 
      initializer: "initialize",
      kind: "uups" // 使用UUPS代理模式
    }
  );

  await auctionFactory.waitForDeployment();
  console.log(`AuctionFactory代理部署完成，地址: ${await auctionFactory.getAddress()}`);
  
  const implementation = await upgrades.erc1967.getImplementationAddress(
    await auctionFactory.getAddress()
  );
  console.log(`AuctionFactory实现合约地址: ${implementation}`);
  
  // 部署CrossChainAuctionManager可升级合约
  console.log("\n部署CrossChainAuctionManager可升级合约...");
  const CrossChainAuctionManager = await ethers.getContractFactory("CrossChainAuctionManager");
  
  const crossChainManager = await upgrades.deployProxy(
    CrossChainAuctionManager,
    [
      deployer.address, // 初始拥有者
      ethers.ZeroAddress, // 路由器地址，稍后可以更新
      ethers.ZeroAddress // 价格预言机地址，稍后可以更新
    ],
    {
      initializer: "initialize",
      kind: "uups" // 使用UUPS代理模式
    }
  );
  
  await crossChainManager.waitForDeployment();
  console.log(`CrossChainAuctionManager代理部署完成，地址: ${await crossChainManager.getAddress()}`);
  
  const crossChainImplementation = await upgrades.erc1967.getImplementationAddress(
    await crossChainManager.getAddress()
  );
  console.log(`CrossChainAuctionManager实现合约地址: ${crossChainImplementation}`);

  // 保存部署地址到文件中
  const fs = require("fs");
  const deploymentInfo = {
    network: hre.network.name,
    auctionNFT: await auctionNFT.getAddress(),
    auctionFactoryProxy: await auctionFactory.getAddress(),
    auctionFactoryImpl: implementation,
    crossChainManagerProxy: await crossChainManager.getAddress(),
    crossChainManagerImpl: crossChainImplementation,
    timestamp: new Date().toISOString(),
  };

  const deploymentPath = `./deployments/${hre.network.name}-deployments.json`;
  fs.mkdirSync("./deployments", { recursive: true });
  fs.writeFileSync(
    deploymentPath,
    JSON.stringify(deploymentInfo, null, 2)
  );
  
  console.log(`\n部署信息已保存至: ${deploymentPath}`);
  console.log("\n部署可升级合约完成！");
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  }); 